const { PARAMS_INCORRECT } = require("../config/error-constants")
const commentService = require("../service/comment.service")

class CommentController {
  async create(ctx, next) {
    // 整理参数
    const { id: userId } = ctx.user
    const { content, momentId, commentId } = ctx.request.body
    if (!content || !momentId) {
      ctx.errorParams = ["content", "momentId"]
      return ctx.app.emit("error", PARAMS_INCORRECT, ctx)
    }
    // 数据库插入数据
    const result = await commentService.create(
      userId,
      content,
      momentId,
      commentId
    )
    ctx.body = {
      code: 0,
      message: "评论成功",
      data: result,
    }
  }

  async remove(ctx, next) {
    const { id: userId } = ctx.user
    const { commentId } = ctx.params
    const result = await commentService.remove(userId, commentId)
    ctx.body = {
      code: 0,
      message: "删除评论成功~",
      data: result,
    }
  }
}

module.exports = new CommentController()
